package com.woniu108.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.mapper.*;
import com.woniu108.car.model.*;
import com.woniu108.car.param.DeleteCarInfoParam;
import com.woniu108.car.param.InsertCarInfoParam;
import com.woniu108.car.param.UpdateCarInfoParam;
import com.woniu108.car.service.CarInfoService;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.dto.CarInfoDto;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements CarInfoService {
    @Resource
    private CarInfoMapper carInfoMapper;
    @Resource
    private CarBrandMapper carBrandMapper;
    @Resource
    private CarSeriesMapper carSeriesMapper;
    @Resource
    private CarDisplacementMapper carDisplacementMapper;
    @Resource
    private CarSeriesYearMapper carSeriesYearMapper;
    @Resource
    private CarStyleMapper carStyleMapper;
    @Resource
    private PersonClient personClient;
    @Resource
    private CarHotMapper carHotMapper;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }


    /**
     * 判断用户
     */
    private void isUser(String userId) {
        // 检查 userId 是否为 null
        if (userId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断用户
        UserInfoDto userInfo = personClient.getByUserId(userId).getData();
        if (userInfo == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
    }

    @Override
    public List<CarInfoDto> loadAllCarInfo(String adminId) {
        //判断管理员权限
        isAdmin(adminId);
        //查carInfo
        List<CarInfo> list = carInfoMapper.selectList(null);
        //设carInfoDto集合
        List<CarInfoDto> dtos = new ArrayList<>();
        for (CarInfo carInfo : list) {
            //设carInfoDto对象
            CarInfoDto dto = BeanUtil.toBean(carInfo, CarInfoDto.class);
            //查汽车品牌信息
            CarBrand carBrand = carBrandMapper.selectById(carInfo.getBrandId());
            dto.setBrandName(carBrand.getBrandName());
            dto.setBrandImage(carBrand.getBrandImage());
            //查汽车系列信息
            CarSeries carSeries = carSeriesMapper.selectById(carInfo.getSeriesId());
            dto.setSeriesName(carSeries.getSeriesName());
            //查汽车排量信息
            CarDisplacement carDisplacement = carDisplacementMapper.selectById(carInfo.getDisId());
            dto.setDisplacementNum(carDisplacement.getDisplacementNum());
            //查生产年份
            CarSeriesYear carSeriesYear = carSeriesYearMapper.selectById(carInfo.getCSId());
            dto.setCSYear(carSeriesYear.getCSYear());
            //查款式
            CarStyle carStyle = carStyleMapper.selectById(carInfo.getStyleId());
            dto.setStyleName(carStyle.getStyleName());
            dto.setStyleDetails(carStyle.getStyleDetails());
            //将dto对象放到集合
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<CarInfoDto> loadCarInfoByUserId(String userId) {
        //判断该id用户是否存在
        isUser(userId);
        //查carInfo
        QueryWrapper<CarInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<CarInfo> list = carInfoMapper.selectList(wrapper);
        //设carInfoDto集合
        List<CarInfoDto> dtos = new ArrayList<>();
        for (CarInfo carInfo : list) {
            //设carInfoDto对象
            CarInfoDto dto = BeanUtil.toBean(carInfo, CarInfoDto.class);
            //查汽车品牌信息
            CarBrand carBrand = carBrandMapper.selectById(carInfo.getBrandId());
            dto.setBrandName(carBrand.getBrandName());
            dto.setBrandImage(carBrand.getBrandImage());
            //查汽车系列信息
            CarSeries carSeries = carSeriesMapper.selectById(carInfo.getSeriesId());
            dto.setSeriesName(carSeries.getSeriesName());
            //查汽车排量信息
            CarDisplacement carDisplacement = carDisplacementMapper.selectById(carInfo.getDisId());
            dto.setDisplacementNum(carDisplacement.getDisplacementNum());
            //查生产年份
            CarSeriesYear carSeriesYear = carSeriesYearMapper.selectById(carInfo.getCSId());
            dto.setCSYear(carSeriesYear.getCSYear());
            //查款式
            CarStyle carStyle = carStyleMapper.selectById(carInfo.getStyleId());
            dto.setStyleName(carStyle.getStyleName());
            dto.setStyleDetails(carStyle.getStyleDetails());
            //将dto对象放到集合
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public Boolean insertCarInfo(InsertCarInfoParam param) {
        //判断用户
        isUser(param.getUserId());
        //判断品牌
        CarBrand carBrand = carBrandMapper.selectById(param.getBrandId());
        if (carBrand == null || Objects.equals(carBrand.getBrandState(), StaticData.CAR_BRAND_STATE_DELETE)) {
            throw new CarException(CarExceptionCode.CAR_BRAND_NOT_HAVE);
        }
        if (Objects.equals(carBrand.getBrandState(), StaticData.CAR_BRAND_STATE_BAN)) {
            throw new CarException(CarExceptionCode.CAR_BRAND_STATE_BAN);
        }
        //新增车辆热度
        CarHot carHot = carHotMapper.selectById(param.getBrandId());
        carHot.setHotNum(carHot.getHotNum()+1);
        carHotMapper.updateById(carHot);
        //判断系列
        //没判断不存在的情况
        CarSeries carSeries = carSeriesMapper.selectById(param.getSeriesId());
        if (carSeries == null || Objects.equals(carSeries.getSeriesState(), StaticData.CAR_SERIES_STATE_DELETE)) {
            throw new CarException(CarExceptionCode.CAR_SERIES_NOT_HAVE);
        }
        if ((Objects.equals(carSeries.getSeriesState(), StaticData.CAR_SERIES_STATE_BAN))) {
            throw new CarException(CarExceptionCode.CAR_SERIES_STATE_BAN);
        }
        //判断款式
        CarStyle carStyle = carStyleMapper.selectById(param.getStyleId());
        if (carStyle == null || Objects.equals(carStyle.getStyleState(), StaticData.CAR_STYLE_STATE_DELETE)) {
            throw new CarException(CarExceptionCode.CAR_STYLE_NOT_HAVE);
        }
        if (Objects.equals(carStyle.getStyleState(), StaticData.CAR_STYLE_STATE_BAN)) {
            throw new CarException(CarExceptionCode.CAR_STYLE_STATE_BAN);
        }
        //判断车牌号
        QueryWrapper<CarInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("car_num", param.getCarNum());
        CarInfo selectOne = carInfoMapper.selectOne(wrapper);
        if (selectOne != null) {
            throw new CarException(CarExceptionCode.CAR_NUM_HAVE);
        }
        //将数据复制到carInfo对象中
        CarInfo carInfo = BeanUtil.toBean(param, CarInfo.class);
        int insert = carInfoMapper.insert(carInfo);
        return insert > 0;
    }

    @Override
    public Boolean updateCarInfo(UpdateCarInfoParam param) {
        //判断用户
        isUser(param.getUserId());
        //判断该车辆是否为当前用户所拥有
        CarInfo carInfo = carInfoMapper.selectById(param.getCarId());
        if (carInfo == null) {
            throw new CarException(CarExceptionCode.CAR_INFO_NOT_HAVE);
        }
        if (!Objects.equals(carInfo.getUserId(), param.getUserId())) {
            throw new CarException(CarExceptionCode.THIS_CAR_NOT_YOU);
        }
        if (param.getMiles().compareTo(carInfo.getMiles()) < 0) {
            throw new CarException(CarExceptionCode.NOT_UPDATE_MILES_SMALL);
        }
        carInfo.setMiles(param.getMiles());
        int update = carInfoMapper.updateById(carInfo);
        return update > 0;
    }

    @Override
    public Boolean deleteCarInfo(DeleteCarInfoParam param) {
        //判断用户
        isUser(param.getUserId());
        //判断该车辆是否为当前用户所拥有
        CarInfo carInfo = carInfoMapper.selectById(param.getCarId());
        if (carInfo == null) {
            throw new CarException(CarExceptionCode.CAR_INFO_NOT_HAVE);
        }
        if (!Objects.equals(carInfo.getUserId(), param.getUserId())) {
            throw new CarException(CarExceptionCode.THIS_CAR_NOT_YOU);
        }
        int delete = carInfoMapper.deleteById(param.getCarId());
        return delete > 0;
    }
}
